<?php

class Lib_Csv
{
    private static $obj;

    private function __construct()
    {
    }

    /**
     * Undocumented function
     *
     * @return Lib_XlsxParse
     */
    public static function getInstance()
    {
        if (is_null(self::$obj)) {
            self::$obj = new self();
        }

        return self::$obj;
    }

    public function __clone()
    {
    }


    public function export($filename, $tileArray=[], $dataArray=[]) {
    
        ob_end_clean();
        ob_start();
        header("Content-Type: text/csv");
        header('Content-Encoding: UTF-8');
        header("Content-Type: text/csv; charset=UTF-8");
        header("Content-Disposition:filename=".$filename);
        $fp=fopen('php://output','w');
        fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF)); //Increase BOM head
        fputcsv($fp,$tileArray);
        $index = 0;
        foreach ($dataArray as $item) {
            if($index == 5000){    //Write a download buffer every 5000 data
                $index=0;
                ob_flush();
                flush();
                //sleep(1);
            }
            $index++;
            fputcsv($fp,$item);
        }
        ob_flush();
        flush();
        ob_end_clean();
    }

}
